/*
 * @Author: szx
 * @Date: 2022-01-04 12:45:47
 * @LastEditTime: 2022-01-22 13:50:29
 * @Description:
 * @FilePath: \leetcode\1-99\53\53.js
 */
/**
 * @param {number[]} nums
 * @return {number}
 */
var maxSubArray = function (nums) {
    let count = 0;
    let res = -Infinity;
    for (let i of nums) {
        count += i;
        if (count > res) {
            res = count;
        }
        if (count <= 0) count = 0;
    }
    return res;
};
/**
 * @param {number[]} nums
 * @return {number}
 */
 var maxSubArray = function (nums) {
    const n = nums.length;
    const dp = new Array(n).fill(0);
    let res = nums[0];
    dp[0] = nums[0];
    for (let i = 1; i < n; i++) {
        dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
        if (dp[i] > res) res = dp[i];
    }
    return res;
};
